#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5+10;
int fa[maxn], d[maxn], num[maxn];
int find(int a){
    return fa[a] == a? a : fa[a] = find(fa[a]);
}
int tot;
map<int, int> id;
int ID(int i){
    if(!id.count(i)) id[i] = tot++;
    return id[i];
}
int main() {
//    ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
    int T; cin >> T;
    for (int kase = 1; kase <= T; ++kase) {
        tot = 0;
        for (int i = 0; i < maxn; ++i) fa[i] = i, num[i] = 1;
        memset(d, 0, sizeof(d));
        id.clear();

        int N; cin >> N;
        for (int i = 0; i < N; ++i) {
            int a, b; cin >> a >> b;
            int ia = ID(a), ib = ID(b);
            if(find(ia)!=find(ib))
                d[fa[ib]] += d[fa[ia]], num[fa[ib]] +=num[fa[ia]], fa[fa[ia]] = fa[ib];
            d[fa[ib]]++;
        }
        set<int> done;
        int ans = 0;
        for (int i = 0; i < tot; ++i) {
            int f = find(i);
            if(!done.count(f)){
                ans += d[f] >= num[f]? num[f]: num[f]-1;
                done.insert(f);
            }
        }
        printf("Case #%d: %d\n", kase, ans);
    }
    return 0;
}